/**
 *
 氪涵官网1.0
 *
 2019-1-16
 *
 公共JS文件
 */
var browser = navigator.appName;
var b_version = navigator.appVersion;
var version = b_version.split(";");
var trim_Version = version[1].replace(/[ ]/g, "");
// if (browser == "Microsoft Internet Explorer" && trim_Version == "MSIE6.0") {
//   window.location.href = './login.html'
// } else if (browser == "Microsoft Internet Explorer" && trim_Version == "MSIE7.0") {
//   window.location.href = './login.html';
// } else if (browser == "Microsoft Internet Explorer" && trim_Version == "MSIE8.0") {
//   window.location.href = './login.html'
// } else if (browser == "Microsoft Internet Explorer" && trim_Version == "MSIE9.0") {
//   window.location.href = './login.html'
// }

function goPAGE() {
  if ((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) {
    // window.location.href = "http://www.xuezaijia.com/mobile/"
  }
}

goPAGE();

var wow = new WOW({
  boxClass: 'wow',
  animateClass: 'animated',
  offset: 0,
  mobile: true,
  live: true
});
wow.init();

/**
 *
 判断导航类名
 *
 @class active
 *
 */

function navActive() {
  var pathName = location.pathname;

  if (pathName.indexOf('index.html') != -1 || pathName.indexOf('html') == -1) {
    //首页
    $(".index").addClass("active").siblings('li').removeClass("active");
  }
  else if (pathName.indexOf('primary.html') != -1) {
    //小学
    $(".specialCourse,.primary").addClass("active").siblings('li').removeClass("active");
  }
  else if (pathName.indexOf('early.html') != -1) {
    //初中
    $(".specialCourse,.early").addClass("active").siblings('li').removeClass("active");
  }
  else if (pathName.indexOf('high.html') != -1) {
    //高中
    $(".specialCourse,.high").addClass("active").siblings('li').removeClass("active");
  }
  else if (pathName.indexOf('fine.html') != -1) {
    //精品课
    $(".fine").addClass("active").siblings('li').removeClass("active");
  }
  else if (pathName.indexOf('teacher.html') != -1) {
    //教研师资
    $(".teacher").addClass("active").siblings('li').removeClass("active");
  }
  else if (pathName.indexOf('bullOpen.html') != -1) {
    //教研师资
    $(".bullOpen").addClass("active").siblings('li').removeClass("active");
  }
  else if (pathName.indexOf('system.html') != -1) {
    //课程体系
    $(".system").addClass("active").siblings('li').removeClass("active");
  }
  else if (pathName.indexOf('teacher-style.html') != -1) {
    //教师风采
    $(".teacher-style").addClass("active").siblings('li').removeClass("active");
  }
  else if (pathName.indexOf('test.html') != -1) {
    //学习力测试
    $(".test").addClass("active").siblings('li').removeClass("active");
  }
  else if (pathName.indexOf('flow.html') != -1) {
    //入学流程
    $(".flow").addClass("active").siblings('li').removeClass("active");
  }
  else if (pathName.indexOf('about-us.html') != -1) {
    //关于我们
    $(".about-us").addClass("active").siblings('li').removeClass("active");
  }
  else if (pathName.indexOf('parents.html') != -1) {
    //家长
    $(".parents").addClass("active").siblings('li').removeClass("active");
  }
  else if (pathName.indexOf('advantage.html') != -1) {
    //嗨课堂优势
    $(".advantage").addClass("active").siblings('li').removeClass("active");
  }
  else if (pathName.indexOf('teaRecource.html') != -1) {
    //教研资源
    $(".teaRecource").addClass("active").siblings('li').removeClass("active");
  }
  else if (pathName.indexOf('news.html') != -1) {
    //嗨课堂资讯
    $(".news").addClass("active").siblings('li').removeClass("active");
  }
  else if (pathName.indexOf('news/1.html') != -1) {
    //嗨课堂资讯
    $(".news").addClass("active").siblings('li').removeClass("active");
  }
  else if (pathName.indexOf('about-us_new.html') != -1) {
    //关于我们
    $(".about-us_new").addClass("active").siblings('li').removeClass("active");
  }
  else if (pathName.indexOf('z/professor.html') != -1) {
    //拜托了教授
    $(".professor").addClass("active").siblings('li').removeClass("active");
  }
}

navActive();
//弹框start
$("body").on('click', ".btn1", function (event) {
  event.preventDefault();
  $(".box").fadeIn();
  $(".Positive").hide();
  $(".other").fadeIn();
  $("#overlay").show();
});
$(".btn1").on('click', function (event) {
  event.preventDefault();
  $(".box").fadeIn();
  $(".Positive").hide();
  $(".other").fadeIn();
  $("#overlay").show();
});
var time;
var num = 0;
var time1 = setTimeout(function () {
  // $('.box').fadeIn();
  // $("#overlay").show();

}, 15000);
$('.close_1,.close').on('click', function () {
  $('.box').fadeOut();
  $("#overlay").fadeOut();
  time = setTimeout(function () {
    $('.box').fadeIn();
    $("#overlay").show();
  }, 45000);

  if (num > 0) {
    clearTimeout(time);
  }
  num++;
  return false;
})
$('.Audition').on('click', function () {
  $(".Positive").hide();
  $('.other').fadeIn();
});
$('.close_1').on('click', function () {
  $('.box').fadeOut(200);
  return false;
});
// $('option').each(function () {
//     if ($(this).val() == '高一') {
//         $(this).attr("selected", true);
//     }
// });
//弹框end
//    获取url携带参数
function GetQueryString(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]);
  return "";
}

//公共功能start
$(function () {

//弹窗表单end
//表单input获得焦点
//   $(".myForm input,#myForm input").focus(function () {
//     $(this).css("borderColor", "#aaa")
//   }).blur(function () {
//     $(this).css("borderColor", "#E1E1E1")
//   })
//表单input
//  首页banner报名
  // 选择年级科目select显示颜色的变化Start
  if ($("#myForm2 .grade").val() == "请选择") {
    $("#myForm2 .grade").css("color", "#aaa");
    $("#myForm2 .grade option").css("color", "#515151");
  } else {
    $("#myForm2 .grade").css("color", "#515151");
    $("#myForm2 .grade option").css("color", "#515151");
  }

  if ($("#myForm2 .kemu").val() == "请选择") {
    $("#myForm2 .kemu").css("color", "#aaa");
    $("#myForm2 .kemu option").css("color", "#515151");
  } else {
    $("#myForm2 .kemu").css("color", "#515151");
    $("#myForm2 .kemu option").css("color", "#515151");
  }
  $("#myForm2 select").on("change", function () {
    if ($("#myForm2 .grade").val() == "请选择") {
      $("#myForm2 .grade").css("color", "#aaa");
      $("#myForm2 .grade option").css("color", "#515151");
    } else {
      $("#myForm2 .grade").css("color", "#515151");
      $("#myForm2 .grade option").css("color", "#515151");
    }

    if ($("#myForm2 .kemu").val() == "请选择") {
      $("#myForm2 .kemu").css("color", "#aaa");
      $("#myForm2 .kemu option").css("color", "#515151");
    } else {
      $("#myForm2 .kemu").css("color", "#515151");
      $("#myForm2 .kemu option").css("color", "#515151");
    }
  })

  var mobileReg = /^1\d{10}$/;//手机号码验证
  var nameReg = /^([\u4e00-\u9fa5]{1,20}|[a-zA-Z\.\s]{1,20})$/;
  $("#myForm2 .name").blur(function(){
    if (!$("#myForm2 .name").val()) {
      // $("#myForm2 .name").css({"border":"1px solid #F21E1E"});
      // $("#myForm2 .nameError").html("*请输入学生姓名").show();
      // return false;
    }
    else{
      if (!nameReg.test($("#myForm2 .name").val())) {
        // $("#myForm2 .name").attr({placeholder: "请输入正确姓名"}).focus().val("");
        $("#myForm2 .name").css({"border":"1px solid #F21E1E"});
        $("#myForm2 .nameError").html("*请输入正确姓名").show();
      }
      else{
        $("#myForm2 .name").css({"border":"1px solid #ccc"});
        $("#myForm2 .nameError").hide();
      }
    }

  });
  $("#myForm2 .mobile").blur(function(){
    if (!$("#myForm2 .mobile").val()) {
      // $("#myForm2 .mobile").css({"border":"1px solid #F21E1E"});
      // $("#myForm2 .mobileError").html("*请输入手机号").show();
    }
    else{
      if (!mobileReg.test($("#myForm2 .mobile").val())) {
        // $("#myForm2 .mobile").attr({placeholder: "手机号不正确"}).focus().val("");
        $("#myForm2 .mobile").css({"border":"1px solid #F21E1E"});
        $("#myForm2 .mobileError").html("*请输入正确手机号").show();
      }
      else{
        $("#myForm2 .mobile").css({"border":"1px solid #ccc"});
        $("#myForm2 .mobileError").hide();
      }
    }
  });
  $('#myForm2 .submit').on('click', function () {
    var $this = $(this);
    // $("#myForm2 .channelAdid").val(GetQueryString("channelAdid"));
    $("#myForm2 .channelAdid").val("6IXRORRXNW");
    var formData = $('#myForm2').serialize();
    $.ajax({
      type: 'get',
      url: CMSAPI + '/register/leads',
      data: formData,
      // dataType: "jsonp",
      // jsonp: "jsoncallback",
      contentType:"application/x-www-form-urlencoded",
      timeout: 5000,
      // 发起请求前就调用
      beforeSend: function (data) {
        if (!$("#myForm2 .name").val()) {
          // $("#myForm2 .name").attr({placeholder: "名字不能为空"}).focus().val("");
          $("#myForm2 .name").css({"border":"1px solid #F21E1E"}).focus();
          $("#myForm2 .nameError").html("*请输入学生姓名").show();
          return false;
        }
        else{
          $("#myForm2 .name").css({"border":"1px solid #ccc"});
          $("#myForm2 .nameError").hide();
        }
        if (!nameReg.test($("#myForm2 .name").val())) {
          // $("#myForm2 .name").attr({placeholder: "请输入正确姓名"}).focus().val("");
          $("#myForm2 .name").css({"border":"1px solid #F21E1E"}).focus();
          $("#myForm2 .nameError").html("*请输入正确姓名").show();
          return false;
        }
        else{
          $("#myForm2 .name").css({"border":"1px solid #ccc"});
          $("#myForm2 .nameError").hide();
        }
        if (!$("#myForm2 .mobile").val()) {
          // $("#myForm2 .name").attr({placeholder: "名字不能为空"}).focus().val("");
          $("#myForm2 .mobile").css({"border":"1px solid #F21E1E"}).focus();
          $("#myForm2 .mobileError").html("*请输入手机号").show();
        }
        else{
          $("#myForm2 .mobile").css({"border":"1px solid #ccc"});
          $("#myForm2 .mobileError").hide();
        }
        if (!mobileReg.test($("#myForm2 .mobile").val())) {
          // $("#myForm2 .mobile").attr({placeholder: "手机号不正确"}).focus().val("");
          $("#myForm2 .mobile").css({"border":"1px solid #F21E1E"}).focus();
          $("#myForm2 .mobileError").html("*请输入正确手机号").show();
          return false;
        }
        else{
          $("#myForm2 .mobile").css({"border":"1px solid #ccc"});
          $("#myForm2 .mobileError").hide();
        }
        if ($("#myForm2 .grade").val() == "请选择" ) {
          alert("请选择年级");
          return false;
        }
        if ($("#myForm2 .kemu").val() == "请选择") {
          alert("请选择科目");
          return false;
        }
        // $this.html('正在提交...').addClass("disabled").attr("disabled", true);
        $this.addClass("disabled").attr("disabled", true);
      },
      success: function (data) {
        if (data.code == 100) {
          $("#myForm2 .name").val("");
          $("#myForm2 .mobile").val("");
          $("#myForm2 .grade").val("请选择");
          $("#myForm2 .kemu").val("请选择");
          $("#myForm2 .grade,#myForm2 .kemu").css("color", "#aaa");
          $("#myForm2 grade option,#myForm2 .kemu option").css("color", "#515151");
          alert('报名成功!');
        } else if (data.code == 101) {
          alert(data.msg);
          $this.removeClass('disabled').attr("disabled", false);
          $("#myForm2 .name").val("");
          $("#myForm2 .mobile").val("");
          return false;
        }
        else {
          alert("预约失败！");
        }
      },
      complete: function () { // 请求完成时会调用
        $(".tip").fadeOut(1500);
        console.log(2);
        // 还原状态
        $("#myForm2 input").val("");
        $this.removeClass('disabled').attr("disabled", false);
      },
      error: function () {
        console.log(3);
      }
    });
  });
})
//公共功能end

//导航动画start
function animate1(name1, name2, left) {
  var cloud = document.getElementById(name1);
  var navBar = document.getElementById(name2);
  //1 给所有li注册事件
  for (var i = 0; i < navBar.children.length; i++) {
    var li = navBar.children[i];
    //
    li.onmouseover = function () {
      //鼠标放到li上，让clound 也到当前li的位置
      animate(cloud, this.offsetLeft);
    };

    li.onmouseout = function () {
      //鼠标离开回到原来的位置
      animate(cloud, left);
    };

    li.onclick = function () {
      left = this.offsetLeft;

    }
  }

  //导航动画
  function animate(element, target) {
    //清除定时器
    if (element.timerId) {
      clearInterval(element.timerId);
    }

    element.timerId = setInterval(function () {
      var current = element.offsetLeft;
      var step = (target - current) / 5;

      step = step > 0 ? Math.ceil(step) : Math.floor(step);
      current += step;
      element.style.left = current + "px";

      if (step === 0) {
        clearInterval(element.timerId);
      }
    }, 15);
  }
}

//导航动画end
//scroll动画Start
function scroll(element, callback) {
  var viewH = $(window).height();//可见高度
  var banner3 = $(element).offset().top;//元素距离顶部的高度
  var banner3Top = $(window).scrollTop();//页面已滚动的高度
  var scrollTop;
  if (banner3 - banner3Top < viewH) {
    setTimeout(function () {
      callback();
    }, 100)
    return false;
  }
  $(window).scroll(function () {
    scrollTop = $(window).scrollTop();
    //banner3 显示动画
    if (banner3 - scrollTop < viewH) {
      callback();
      return;
    }
  });
}

//scroll动画End
//滚动动画
// function () {
//     var viewH =$(window).height();//可见高度
//     var banner3 = $(element).offset().top;
//     //banner3 显示动画
//     // if(banner3-$(document).scrollTop()<viewH){
//     //    $(".banner3 .bottom").css({opacity:1,filter: "alpha(opacity=1)"}).find(".right").css("height","200px");
//     // }else {
//     //    $(".banner3 .bottom").css({opacity:1,filter: "alpha(opacity=1)"});
//     // }
// }

// 导航底部
// $(function () {
//     $(".header .content .left ul li").each(function (i, v) {
//         var $this = $(this);
//         if($this.find("a").hasClass("color_red1")){
//             $this.find("span").css("width","100%");
//         }
//     })
//
// })